package com.hdjk.view.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Constant;
import com.hdjk.view.dialog.UpdateDialog;
import com.hdjk.view.network.Network;
import com.hdjk.view.util.CommonUtil;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.PermissionUtils;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity implements UpdateDialog.UpdateDialogListener {

	private UpdateDialog mUpdateDialog = null;
	private long mThreadTime = 0;

	@Override
	protected void initView(Bundle savedInstanceState) {
		if (android.os.Build.VERSION.SDK_INT > 18) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
		setContentView(R.layout.activity_splash);
		ImageView iv_wel=(ImageView) findViewById(R.id.iv_wel);
		iv_wel.setImageResource(R.drawable.img_splash);
	}

	@Override
	protected void initData() {
		PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, getPermission);
	}
	
	private void initVariable() {
		mThreadTime = System.currentTimeMillis();
		//检查版本,是否需要更新
		Network.checkUpdate(new CallBackListener() {
			@Override
			public void onCallBackCompleted(Msg msg) {
				boolean flag = false;
				String[] infos = null;

				if (msg != null) {
					if (msg.getSuccess() && msg.getData() != null) {
						infos = (String[]) msg.getData();
						try {
							if (!TextUtils.isEmpty(infos[4])) {
								int code = Integer.valueOf(infos[4]);
								if (code > CommonUtil.getVersionCode(SplashActivity.this)) {
									flag = true;
								}
							}
						} catch (Exception e) {
						}
					}
				}

				long duration = 2000 - (System.currentTimeMillis() - mThreadTime);
				final boolean isUpdate = flag;
				final String[] updateInfo = infos;
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						if (isUpdate) {
							if (mUpdateDialog == null) {
								mUpdateDialog = new UpdateDialog(SplashActivity.this, SplashActivity.this);
							}
							// 0：描述 1:是否强制更新 2:下载链接地址 3:标题
							if (updateInfo != null) {
								mUpdateDialog.showDialog(updateInfo[0], updateInfo[1], updateInfo[2], "版本更新提示");
							}
						} else {
							toWitchPage();
						}
					}
				}, duration > 0 ? duration : 0);
			}

			@Override
			public void onCallBackError(Msg msg) {

			}
		});
	}

	private void toWitchPage() {
		String firstVersion = UserTokenUtil.getFirstVersion(this);
		if (TextUtils.isEmpty(firstVersion) || !firstVersion.equals(Constant.FIRST_VERSION)) {
			UserTokenUtil.setFirstVersion(this, Constant.FIRST_VERSION);
			Intent intent = new Intent(SplashActivity.this, FirstInstallActivity.class);
			startActivity(intent);
		} else {
//			if (UserTokenUtil.isLogin(this)) {
				Intent intent = new Intent(SplashActivity.this, MainTabActivity.class);
				startActivity(intent);
//			} else {
//				Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
//				startActivity(intent);
//			}

			if (null != UserTokenUtil.getToken(this) && !UserTokenUtil.getToken(this).equals("")) {
				UserTokenUtil.getNewToken();
			}
		}
		SplashActivity.this.finish();
	}
	
	@Override
	public void onBackPressed() {
		return;
	}


	private PermissionUtils.PermissionGrant getPermission = new  PermissionUtils.PermissionGrant(){
		@Override
		public void onPermissionGranted(int requestCode, String msg) {
			switch (requestCode) {
				case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
					//设置授权成功后的回调，可以是一个提醒或一个操作等
					initVariable();
					break;
			}
		}

		@Override
		public void onPermissionDeny(int requestCode, String msg) {
			switch (requestCode) {
				case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
					//拒绝授权后的回调，可以是一个提醒或一个操作等
					initVariable();
					break;
			}
		}
	};

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, getPermission);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode){
			case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
				if (getPermission!=null){
					if (PermissionUtils.checkPermission(this, requestCode)){
						//设置授权成功后的回调，可以是一个提醒或一个操作等
						getPermission.onPermissionGranted(requestCode,"form app systemSetPage");
					}else{
						//拒绝授权后的回调，可以是一个提醒或一个操作等
						getPermission.onPermissionDeny(requestCode,"form app systemSetPage");
					}
				}
				break;
		}
	}

	@Override
	protected void onDestroy() {
		if (mUpdateDialog != null) {
			mUpdateDialog.onDestroy();
			mUpdateDialog.dismiss();
			mUpdateDialog = null;
		}
		super.onDestroy();
	}

	@Override
	public void onCancel() {
		toWitchPage();
	}

	@Override
	public void onOk() {
	}
}